 package srdb.migration.dbhandler.redwood;
 
 import srdb.migration.Utility;
import srdb.migration.common.MTKConstants;
import srdb.migration.schema.metadata.Column;
import srdb.migration.schema.metadata.Function;
import srdb.migration.schema.metadata.Grant;
import srdb.migration.schema.metadata.ObjectType;
import srdb.migration.schema.metadata.Package;
import srdb.migration.schema.metadata.Procedure;
import srdb.migration.schema.metadata.Role;
import srdb.migration.schema.metadata.Synonym;
import srdb.migration.schema.metadata.Trigger;
 
 public class EDBTransformDDL extends PGTransformDDL
 {
   protected String getColumnLine(Column column)
   {
     String str = "";
     String colName = column.getName();
 
     if ((!column.isCaseSensitive()) && (colName.indexOf('#') >= 0)) {
       colName = "\"" + colName.toLowerCase() + "\"";
     }
 
     str = str + getIndent() + colName;
 
     String customType = null;
 
     if ((customType = super.getCustomColType(column.getTableName(), column.getName())) != null) {
       str = str + " " + customType;
     } else {
       if (column.isRowID())
       {
         str = str + " " + "VARCHAR";
       } else if (column.isInterval())
         str = str + " " + "INTERVAL";
       else if (column.isTimeStamp())
       {
         str = str + " " + column.getDataTypeString().replaceAll("LOCAL", "");
       }
       else str = str + " " + column.getDataTypeString();
 
       if ((column.getDataType() == Column.DataType.VARCHAR) || (column.getDataType() == Column.DataType.NVARCHAR)) {
         str = str + "(" + column.getLength() + ")";
       }
       if (((column.getDataType() == Column.DataType.INTEGER) || (column.getDataType() == Column.DataType.NUMERIC)) && 
         (column.getPrecision() > 0)) {
         str = str + "(" + column.getPrecision();
         if (column.getScale() > 0)
           str = str + "," + column.getScale();
         str = str + ")";
       }
 
     }
 
     if (column.getDefaultClause() != null) {
       str = str + " DEFAULT " + column.getDefaultClause();
     }
 
     if (!column.isNullable()) {
       str = str + " NOT NULL";
     }
     return str;
   }
 
   public String getCreateScript(Procedure proc)
   {
     String sql = proc.getSQL();
 
     sql = sql.replaceFirst("AUTHID", "-- AUTHID");
     sql = "CREATE " + sql + "\n";
 
     return sql;
   }
 
   public String getCreateScript(Function func)
   {
     String header = "";
     String modHeader = "";
     String body = "";
     String sql = "";
 
     int headerPos = func.getSQL().toUpperCase().indexOf("RETURN");
 
     header = func.getSQL().substring(0, headerPos);
     body = func.getSQL().substring(headerPos);
 
     body = body.replaceFirst("AUTHID", "-- AUTHID");
 
     if (header.toUpperCase().contains("DEFAULT")) {
       modHeader = stripDefaultClause(header);
 
       sql = sql + "CREATE " + modHeader;
     } else {
       sql = "CREATE " + header;
     }
 
     sql = sql + " " + body;
 
     sql = sql + "\n";
 
     return sql;
   }
 
   public String getCreateScript(Synonym syn)
   {
     String sql = "CREATE OR REPLACE " + (syn.isPublic() ? "PUBLIC" : "") + " SYNONYM " + syn.getSynname() + "\n";
     sql = sql + getIndent() + " FOR " + syn.getSynobjname() + ";\n";
     return sql;
   }
 
   public String getCreateScript(Trigger trigger)
   {
     String sql = "CREATE TRIGGER " + trigger.getName() + " \n";
 
     if (trigger.isBeforeTrigger())
       sql = sql + getIndent() + "BEFORE ";
     else {
       sql = sql + getIndent() + "AFTER ";
     }
 
     String triggerTableName = trigger.getTableName();
     if (Utility.isCaseSensitive(triggerTableName, MTKConstants.DATABASE_TYPE.ORACLE)) {
       triggerTableName = "\"" + triggerTableName + "\"";
     }
 
     sql = sql + trigger.getEvent() + " ON " + triggerTableName + "\n";
 
     if (trigger.isRowLevelTrigger()) {
       sql = sql + "FOR EACH ROW \n";
     }
 
     return sql + trigger.getBody();
   }
 
   public String getCreateScriptPackageSpec(Package pack)
   {
     String str = "";
     String sql = "";
 
     str = pack.getHeaderSQL();
 
     if (str.toUpperCase().contains("DEFAULT"))
     {
       sql = sql + "CREATE " + str;
     }
     else sql = "CREATE " + str;
 
     int lastEnd = sql.toUpperCase().lastIndexOf("END");
     sql = sql.substring(0, lastEnd + 3);
 
     sql = sql + ";\n";
     return sql;
   }
 
   public String getCreateScriptPackageBody(Package pack)
   {
     String sql = "";
 
     if (pack.getBodySQL().compareTo("") == 0) {
       return "";
     }
     sql = pack.getBodySQL();
 
     sql = sql.replaceAll("AUTHID", "-- AUTHID");
 
     sql = "CREATE " + sql;
 
     sql = sql + "\n";
 
     return sql;
   }
 
   public String getCreateScript(Role role)
   {
     String sql = "";
     sql = sql + "Create role " + role.getRoleName() + " with NOLOGIN";
 
     if (role.isDba()) {
       sql = sql + " SUPERUSER ";
     }
 
     sql = sql + ";";
 
     return sql;
   }
 
   public String getCreateScript(Grant grant)
   {
     String sql = "GRANT ";
 
     if (grant.getPrivilegeType() == Grant.PRIV_TYPE.OBJECT) {
       sql = sql + grant.getPrivilege() + " ON " + grant.getOwner().toLowerCase() + "." + grant.getObjectName();
       sql = sql + " TO " + grant.getGrantee() + ";\n";
 
       sql = sql + "GRANT USAGE ON SCHEMA " + grant.getOwner().toLowerCase() + " TO " + grant.getGrantee() + ";";
     }
     else {
       sql = sql + grant.getObjectName() + " TO " + grant.getGrantee() + ";\n";
     }
 
     return sql;
   }
 
   public String getCreateScript(ObjectType objectType)
   {
     String sql = "CREATE OR REPLACE " + objectType.getBody();
 
     if (!sql.endsWith(";")) {
       sql = sql + ";";
     }
 
     sql = sql + "\n";
     return sql;
   }
 }

/* 

 * 
 */